import 'package:flutter/material.dart';
import 'package:flutter_picker/Picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zchope_student/beans/citybean.dart';
import 'package:zchope_student/beans/eventbean.dart';
import 'package:zchope_student/utils/mydio.dart';
import 'package:zchope_student/utils/utils.dart';
import 'package:zchope_student/view/textfield.dart';

import '../apis.dart';
import '../const.dart';

class UserBind extends StatefulWidget {
  @override
  _UserBindState createState() => _UserBindState();
}

class _UserBindState extends State<UserBind> {
  Map<String, dynamic> dataBind = Map();
  SharedPreferences sharedPreferences;
  String memberBh = '';
  String loginName = '';
  var _areaCode;
  var _area = '选择城市';
  var _uuid;
  var pickerCity;
  var _scaffoldkey = new GlobalKey<ScaffoldState>();

  _setPersonNum() {
    return EditText(
      keyboardType:TextInputType.number,
      data: dataBind,
      name: 'personNum',
      labelText: '请输入二代身份证号',
      value: Utils.getInstance().checkEmpty(dataBind['personNum'])
          ? ''
          : dataBind['personNum'],
    );
  }

  @override
  void initState() {
    _getMemberhb();
    _buildPickerCity();
  }

  // 获取 areacode 和 学员编号
  _getMemberhb() async {
    sharedPreferences = await SharedPreferences.getInstance();
    _areaCode = sharedPreferences.get(Const.AREA_CODE);
    _area = Utils.getInstance().getCityName(_areaCode);
    _uuid = sharedPreferences.getString(Const.UUID);
    dataBind['memberBh'] = _uuid;
  }

  _buildPickerCity() {
    pickerCity = Picker(
      headercolor: Const.black,
      containerColor: Const.black,
      backgroundColor: Const.black,
      adapter: PickerDataAdapter(data: _setPickerCities()),
      title: Text(
        '请选择',
        style: TextStyle(color: Const.gold),
      ),
      cancelText: '取消',
      cancelTextStyle: TextStyle(color: Const.gold),
      confirmText: '确定',
      confirmTextStyle: TextStyle(color: Const.gold),
      textStyle: TextStyle(color: Const.gold),
      onConfirm: (Picker picker, List value) {
        _area = Const.cities[value[0]].name;
        _areaCode = Const.cities[value[0]].code;
        dataBind['areacode'] = Const.cities[value[0]].code;
        setState(() {});
      },
    );
  }

  _setPickerCities() {
    List<PickerItem<dynamic>> pickerItems = List();
    for (CityBean bean in Const.cities) {
      pickerItems.add(PickerItem(
          value: Icons.add,
          text: Text(bean.name, style: TextStyle(color: Const.gold))));
    }
    return pickerItems;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Const.bar,
      key: _scaffoldkey,
      appBar: AppBar(
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              pickerCity.show(_scaffoldkey.currentState);
            },
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: SizedBox(
                height: 60,
                child: Center(
                  child: Text(
                    _area,
                    style: TextStyle(fontSize: 15, color: Const.gold),
                  ),
                ),
              ),
            ),
          )
        ],
        backgroundColor: Const.bar,
        centerTitle: true,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 33),
            child: _setPersonNum(),
          ),
          MaterialButton(
            height: 68,
            minWidth: MediaQuery.of(context).size.width,
            elevation: 0,
            onPressed: () {
              if (Utils.getInstance().checkEmpty(_areaCode)) {
                Utils.getInstance().showSnackBar(_scaffoldkey, '请先选择城市');
                return;
              }

              MyDio.getInstance().clearInstance();
              MyDio.getInstance().post(Apis.bind, (data) async {
                print(dataBind);
                Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);
                if (data['resultCode'] == 0) {
                  sharedPreferences.setString(
                      Const.PERSON_NUM, dataBind['personNum']);
                  sharedPreferences.setString(Const.AREA_CODE, _areaCode);
                  eventBus.fire(EventBean(title:Const.EVENT_BIND));
                  Navigator.pop(context);
                }
              }, params: dataBind);
            },
            child: Text(
              "确    认",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            color: Const.gold,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(30),
                    bottomRight: Radius.circular(30))),
          ),
        ],
      ),
    );
  }

//  void _saveCityAndCode(String cityPicked, String cityPickedCode) async {
//    SharedPreferences sp = await SharedPreferences.getInstance();
//    sp.setString('cityName', cityPicked);
//    sp.setString('areacode', cityPickedCode);
//  }
}
